#!/usr/bin/env python
# coding: utf-8

# # 地理编码

# In[5]:


import requests,json
import pandas as pd
lin_key = "65b04d34830504b4f0a6fe32550d797a"


# In[6]:


中大南方_address = "广东省广州市中山大学南方学院"


# In[7]:


url = "https://restapi.amap.com/v3/geocode/geo?parameters"
params = {
    "key":lin_key,
    "address":中大南方_address
}
# positional argument follows keyword argument
# pa 一定要放在前面，keyword argument放在末尾！！！可以
response = requests.get(url,params=params)
response


# In[8]:


中大南方_location = response.json()['geocodes'][0]['location']
中大南方_location


# # 逆地理编码

# In[9]:


def regeo(key,location,extensions="base",radius=None,poitype=None):
    """"""
    url = "https://restapi.amap.com/v3/geocode/regeo?parameters"
    params = {
        "key":key,
        "location":location,
        "extensions":extensions,
        "radius":radius,
        "poitype":poitype
    }
    r = requests.get(url,params)
    results = r.json()
    return results


# In[10]:


url = "https://restapi.amap.com/v3/geocode/regeo?parameters"
params = {
    "key":lin_key,
    "location":中大南方_location,
    "extensions":"all"
}
r = requests.get(url,params)
r


# In[11]:


_roads_ = []
for item in r.json()['regeocode']['roads']:
#     print(item['name'])
    _roads_.append(item['name'])
_roads_


# In[12]:


r.json()


# In[13]:


pd.json_normalize(r.json()['regeocode']['pois'])


# # 路径规划

# In[14]:


# 数据准备：
origin = "广东省广州市中山大学南方学院"
destination = "广东省广州市天河区天河体育中心"


# In[15]:


def geocode(key,address,city=None,batch=None)->str:
    url = "https://restapi.amap.com/v3/geocode/geo?parameters"
    params = {
        "key":key,
        "address":address,
        "city":city,
        "batch":batch
    }
    response = requests.get(url,params=params)
    results = response.json()['geocodes'][0]['location']
    # 返回值
    return results


# In[16]:


help(geocode)


# In[17]:


起始点_中大南方 = geocode(lin_key,origin)
起始点_中大南方


# In[18]:


目的地_天体 = geocode(xu_key,destination)
目的地_天体


# In[19]:


url = "https://restapi.amap.com/v3/direction/walking?parameters"
params = {
    "key":lin_key,
    "origin":起始点_中大南方,
    "destination":目的地_天体
}
r = requests.get(url,params)
r


# In[20]:


r.json()['route']['paths']


# In[ ]:




